package com.heima.article.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.article.mapper.ApArticleContentMapper;
import com.heima.article.pojo.ApArticleContent;
import com.heima.article.service.ApArticleContentService;
import com.heima.model.media.dtos.WmNewsResultDTO;
import org.springframework.stereotype.Service;

/**
 * <p>
 * APP已发布文章内容表 服务实现类
 * </p>
 *
 * @author HM
 * @since 2023-03-22
 */
@Service
public class ApArticleContentServiceImpl extends ServiceImpl<ApArticleContentMapper, ApArticleContent> implements ApArticleContentService {

    @Override
    public void createArticleContent(Long articleId, WmNewsResultDTO dto) {
        // 先查询，有则update，没有则insert
        LambdaQueryWrapper<ApArticleContent> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ApArticleContent::getArticleId, articleId);
        ApArticleContent contentDB = this.getOne(queryWrapper);

        if (contentDB == null) {
            ApArticleContent insertContent = new ApArticleContent();
            insertContent.setArticleId(articleId);
            insertContent.setContent(dto.getContent());
            this.save(insertContent);
        } else {
            ApArticleContent updateContent = new ApArticleContent();
            updateContent.setArticleId(articleId);
            updateContent.setContent(dto.getContent());
            this.updateById(updateContent);
        }
    }
}
